<?php

class Admin_IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */    	
    }

	public function indexAction()
    {
        // action body
        $layout = $this->_helper->layout();
        $layout->title =  "站点级别";   
        $layout->keywords = "站点,级别,站点级别";
        $layout->description = "站点级别";        
        $this->view->tableTile = "站点级别";     
                
    }
    
	public function Islogined()
    {   	
    	$authNamespace = new Zend_Session_Namespace('HoldBB_Auth');
		if (isset($authNamespace->user)){
			return true;	
		}	
		else {
			$this->_redirect('/admin/index/login');		
		}		
    }
    
 	public function logoutAction()
    {   	
    	$authNamespace = new Zend_Session_Namespace('HoldBB_Auth');
		$authNamespace->unsetAll();		
		$this->_redirect('/');
        return;
    }
    
    public function loginAction()
    {   	
    	$this->_helper->layout->disableLayout();
    }
    
    //初始化ALC---从数据库获取，获取该用户的角色，并对其权限初始化。
    public function initALC()
    {   	
    	$this->_helper->layout->disableLayout();
    }
    
	public function chekloginAction()
    { 
        if ($this->getRequest()->isPost()) { 
        	$request = $this->getRequest();
        	$db = $this->_getParam('db');
        	if (Zend_Registry::isRegistered('db')){
        		$db = Zend_Registry::get('db');
        	} else {
	        	$bootstrap = $this->getFrontController()->getInstance()->getParam('bootstrap');			
	        	//初始化，配置数据库
				$resource = $bootstrap->getPluginResource('db');
				$db = $resource->getDbAdapter();
				Zend_Registry::set('db',$db);	
        	}         	
        	
        	$UserName = $request->getParam('LoginID');
			$Password = $request->getParam('Password');	
			
			
			if ((empty($UserName)) || (empty($Password)))
			{
				$this->_helper->FlashMessenger('用户名 或者密码为空！');                
                $this->_redirect('/');
			}				
			
            $adapter = new Zend_Auth_Adapter_DbTable(
                $db,
                'admins',
                'adminname',
                'adminpass',
                'MD5(?)'
                );
 				//'MD5(CONCAT(?, password_salt))'
            $adapter->setIdentity($UserName);
            $adapter->setCredential($Password);
 
            $auth   = Zend_Auth::getInstance();
            $result = $auth->authenticate($adapter);
 
            if ($result->isValid()) {            	
            	$authNamespace = new Zend_Session_Namespace('HoldBB_Auth');
				$authNamespace->user = $UserName;
				$authNamespace->setExpirationSeconds(3600);
            	
            	
            	/*$params = $request->getParams();
		   		$module = $params["module"];
		        $controller = $params["controller"];
		        $action = $params["action"];
		
		        $this->view->targetUrl = $this->getFrontController()->getBaseUrl()."/$module/$controller/";
		        
		        $this->view->state = 1;
		        $this->view->infotitle = '提示';
		        $this->view->message = '登录成功！';
		        $this->render('nav/info');*/ 
                $this->_helper->FlashMessenger('登录成功！');
                $this->_redirect('/admin/nav/index');
                return;
            } 
            else {
            	$this->_helper->FlashMessenger('登录失败！');
                $this->_redirect('/');
                return;
            }
        }        	
    	
    }
    
    public function ajaxchekloginAction()
    {    	 
    	$this->_helper->layout->disableLayout();    //disable layout
    	$this->_helper->viewRenderer->setNoRender(); //suppress auto-rendering
    	
        if ($this->getRequest()->isPost()) { 
        	$request = $this->getRequest();
        	$db = $this->_getParam('db');
        	if (Zend_Registry::isRegistered('db')){
        		$db = Zend_Registry::get('db');
        	} else {
	        	$bootstrap = $this->getFrontController()->getInstance()->getParam('bootstrap');			
	        	//初始化，配置数据库
				$resource = $bootstrap->getPluginResource('db');
				$db = $resource->getDbAdapter();
				Zend_Registry::set('db',$db);	
        	}         	
        	
        	$UserName = $request->getParam('LoginID');
			$Password = $request->getParam('Password');		
			
            $adapter = new Zend_Auth_Adapter_DbTable(
                $db,
                'admins',
                'adminname',
                'adminpass',
                'MD5(?)'
                );
 				//'MD5(CONCAT(?, password_salt))'
            $adapter->setIdentity($UserName);
            $adapter->setCredential($Password);
 
            $auth   = Zend_Auth::getInstance();
            $result = $auth->authenticate($adapter);
 
            if ($result->isValid()) {            	
            	$authNamespace = new Zend_Session_Namespace('HoldBB_Auth');
				$authNamespace->user = $UserName;
				$authNamespace->setExpirationSeconds(3600);
            	                
				$data = array();
				$data['status'] = true;
				$json = Zend_Json::encode($data);//basically, $data array will also be available in the JS.
				//$this->getHelper('Json')->sendJson(Zend_Json::encode($data));
				//$this->_helper->json(Zend_Json::encode($data));
				echo $json; //this will echo JSON to the Javascript 
				unset($json);
				unset($data);				
            } 
            else {
            	$data = array();
				$data['status'] = false;
				$json = Zend_Json::encode($data);//basically, $data array will also be available in the JS.
				echo $json; //this will echo JSON to the Javascript
				unset($json);
				unset($data);	
            }
    	} 
    	
    }
    
}

